<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="600px"
    draggable
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
      style="width: 90%"
    >
      <el-form-item label="页面名称" prop="pageName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.pageName"
          placeholder="请输入页面名称"
          clearable
        />

        <span v-else>{{ formData.pageName }}</span>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          placeholder="请输入备注"
          type="textarea"
          show-word-limit
          :maxlength="250"
          :autosize="{ minRows: 3, maxRows: 4 }"
          clearable
        />

        <span v-else>{{ formData.remarks || "无" }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { useRoute } from "vue-router";
const route = useRoute();
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  pageName: [
    { required: true, message: "请填写工作台编号", trigger: "blur" },
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  pageName: "",
  remarks: "",
  workbenchId: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    formData.workbenchId = route.query.workbenchId;
    if (newVal?.id) {
      const { id, pageName, remarks, workbenchId } = newVal;
      Object.assign(formData, {
        id,
        workbenchId,
        pageName,
        remarks,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.pageName = "";
  formData.remarks = "";
}

const loading = ref(false);

/** 工作台信息保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      formData.workbenchId = route.query.workbenchId;
      await request({
        url: "/system/sysworkbenchpage/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
